const responseMessage = require("../response/response_message")
const {
    getGroupsApi,
    getGroupByNumberApi,
    addGroupMemberApi,
    addGroupApi
} = require("../papi/group_api")

/**
 * 获取某一个登录用户的群列表
 */
exports.get_groups = async (ctx) => {
    try{
        const { _id } = ctx.request.query;
        const groups = await getGroupsApi(_id);
        ctx.body = new responseMessage(201,"success",groups);
    }catch(error){
        ctx.body = new responseMessage(500,error.message,{});
    }
};

/**
 * 获取群号查询一个群 
 */
exports.get_group =  async (ctx) => {
    try{
        const { group_number } = ctx.request.query;
        const group = await getGroupByNumberApi(group_number);
        ctx.body = new responseMessage(201,"success",group);
    }catch(error){
        ctx.body = new responseMessage(500,error.message,{});
    }
}

/**
 * 添加一个人到群组内
 */
exports.add_group_member = async (ctx) => {
    try{
        const { fromId,groupId } = ctx.request.body
        const {modifiedCount} = await addGroupMemberApi(fromId,groupId);
        if(modifiedCount > 0){
            return ctx.body = new responseMessage(201,"success",{});
        }else{
            return ctx.body = new responseMessage(201,"已存在此群组中",{});
        }
    }catch(error){
        return ctx.body = new responseMessage(500,error.message,{})
    }
}

exports.add_group = async (ctx) => {
    try{
        const group = ctx.request.body;
        const groupResult = await addGroupApi(group);
        if(groupResult){
            ctx.body = new responseMessage(201,"success",groupResult)
        }else{
            ctx.body = new responseMessage(201,"添加失败")
        }
    }catch(error){
        ctx.body = new responseMessage(500,error.message,{})
    }
}